<template>
  <div class="home-page">
    <!-- 轮播图 -->
    <van-swipe :autoplay="3000">
      <van-swipe-item v-for="item in images" :key="item.id">
        <img v-lazy="'http://liufusong.top:8080'+item.imgSrc" />
      </van-swipe-item>
    </van-swipe>

    <!-- 搜索框 -->
    <my-search class="home_search"></my-search>

    <!-- 分类，宫格 -->
    <van-grid>
      <van-grid-item icon="wap-home" text="整租" />
      <van-grid-item icon="friends" text="合租" />
      <van-grid-item icon="map-marked" text="地图找房" />
      <van-grid-item icon="shop-collect" :to="user?'/rent/add':'/login'" text="去出租" />
    </van-grid>

    <!-- 租房小组 -->
    <div class="group">
      <!-- title -->
      <div class="group_title">
        <span class="group_title1">租房小组</span>
        <span class="more">更多</span>
      </div>
      <!-- body -->
      <div class="group_body">
        <div class="group_body_info" v-for="item in groupList" :key="item.id">
          <div class="group_body_ele">
            <img :src="'http://liufusong.top:8080'+item.imgSrc" alt="">
            <div>
              <p>{{item.title}}</p>
              <p>{{item.desc}}</p>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import MySearch from '@/components/MySearch'
import { mapState } from 'vuex'
import { getSwiperImg, getGroup } from '@/api/home.js'
export default {
  name: 'Home',
  data () {
    return {
      images: [],
      groupList: []
    }
  },
  components: { MySearch },
  computed: {
    ...mapState(['cityInfo', 'user'])
  },
  created () {
    this.getSwiperImg()
    this.getGroup()
  },

  methods: {
    async getSwiperImg () { // 获取轮播图数据
      try {
        const res = await getSwiperImg()
        // console.log(res.data.body)
        this.images = res.data.body
      } catch (error) {
        console.log('获取轮播图失败')
      }
    },
    async getGroup () { // 获取租房小组数据
      try {
        const res = await getGroup(this.cityInfo.id)
        // console.log(res)
        this.groupList = res.data.body
      } catch (error) {
        console.log('获取租房小组数据失败')
      }
    }
  }
}
</script>

<style scoped lang='less'>
.home-page{
  position: relative;
  /deep/.van-swipe{
    height: 212px;
    img{
      width: 100%;
      height: 100%;
    }
  }
  /deep/.van-swipe__indicator{
    width: 8px;
    height: 8px;
    background-color: #999;
  }
  // 搜索框样式
  .home_search{
    position:absolute;
    z-index: 5;
    width: 100%;
    top: 0;
    background: rgba(0, 0, 0, 0);
    padding: 25px 10px 0 20px;
  }
  // 租房小组样式
  .group{
    padding-left:10px;
    background-color: #f6f5f6;
    overflow: hidden;
    .group_title{
      margin: 15px 0 15px 10px;
      display: flex;
      justify-content: space-between;
      .group_title1{
        font-size: 15px;
        font-weight: 700;
      }
      .more{
        padding-right: 10px;
        color: #787d82;
        font-size: 14px;
        font-weight: 400;
      }
    }
    .group_body{
      display: flex;
      flex-wrap: wrap;
      .group_body_info{
        width: 50%;
        padding-right: 10px;
        box-sizing: border-box;
        .group_body_ele{
          margin-bottom: 10px;
          padding-bottom: 10px;
          display: flex;
          background-color: #fff;
          img{
            display: block;
            width: 50px;
            height: 50px;
            margin: 0 10px;
          }
          div{
            display: flex;
            // align-items: center;
            flex-direction: column;
            justify-content: center;
            p{
              margin: 0;
              font-size: 14px;
              line-height: 1.15;
            }
          }
        }
      }

    }
  }
}

</style>
